package com.cndragon.baby.notification
{
	import flash.utils.getQualifiedClassName;

	public class LogicMgr
	{
		static private var _instance:LogicMgr;
		
		private var logicList:Array;
		
		public function LogicMgr(s:S)
		{
			logicList = [];
		}
		
		static public function getInstance():LogicMgr
		{
			if(_instance==null){
				_instance = new LogicMgr(new S());
			}
			return _instance;
		}
		
		
		public function registerLogic(logic:SimpleLogic):void
		{
			if(logicList[logic.getLogicName()]==null)
			{
				logicList[logic.getLogicName()] = logic;
				logic.onRegister();
			}
			else
			{
				throw("error:逻辑"+logic.getLogicName()+"已经注册过了");
			}
		}
		
		
		
		public function unRegisterLogic(logicName:String):void
		{
			if(logicList[logicName]!=null)
			{
				(logicList[logicName] as SimpleLogic).onRemove();
				logicList[logicName] = null;
			}
			else
			{
				throw("error:逻辑"+logicName+"不在逻辑列表里");
			}
		}
		
		
		/**
		 * 获得注册过这个notificationID的logic数组
		 * @param notificationID
		 * @return 
		 * 
		 */		
		public function getLogicListByNotificationID(notificationID:int):Array
		{
			var arr:Array = [];
			for each (var logic:SimpleLogic in logicList) 
			{
				if(logic.listNotificationInterests() == null)
				{
					throw("error: "+ getQualifiedClassName(logic) +" 没有关注notification，需要实现listNotificationInterests方法");
				}
				if(logic.listNotificationInterests().indexOf(notificationID)!=-1)
				{
					arr.push(logic);
				}
			}
			return arr;
		}
		
		
	}
}

class S{}